package com.example.keli.ui;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.example.keli.R;
import com.example.keli.api.HttpUtils;
import com.example.keli.api.RetrofitManager;
import com.example.keli.base.AppConfig;
import com.example.keli.base.BaseActivity;
import com.example.keli.base.utils.ToastUtil;
import com.example.keli.base.widget.ClearEditText;
import com.example.keli.ui.register.VerifyActivity;
import com.flyco.roundview.RoundTextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import com.example.keli.api.http.api.BaseResponse;
import com.example.keli.api.http.api.subscriber.SubscriberListener;

public class LoginActivity extends BaseActivity {

    @BindView(R.id.close)
    ImageView mClose;
    @BindView(R.id.phone)
    ClearEditText mPhone;
    @BindView(R.id.next)
    RoundTextView mNext;

    @Override
    protected int layoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void bindData() {
        EventBus.getDefault().register(this);
    }

    @OnClick({R.id.close, R.id.next})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.close:
                finish();
                break;
            case R.id.next:
                if(TextUtils.isEmpty(mPhone.getText().toString().trim())){
                    ToastUtil.show("请输入手机号");
                    return;
                }
                loadDatas();
//                toActivity(MainActivity.class);
                break;
        }
    }

    private void loadDatas() {
        startProgressDialog();
        Map<String, String> map = new HashMap<>();
        map.put("address", mPhone.getText().toString().trim());
        map.put("type", "login");
        HttpUtils.ApiFunc(RetrofitManager.mApiService.sendSms(map), new SubscriberListener<BaseResponse<Object>>() {
            @Override
            public void onSuccess(BaseResponse<Object> rewardListBaseResponse) {
                stopProgressDialog();
                VerifyActivity.start(LoginActivity.this,mPhone.getText().toString().trim());
            }

            @Override
            public void onFailer(String msg) {
                stopProgressDialog();
                ToastUtil.show(msg);
            }

            @Override
            public void onTokenError() {

            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event(String close_login){
        if(TextUtils.equals(close_login, AppConfig.CLOSE_LOGIN)){
            //关闭界面
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
